<?php

/**
 * 分类管理控制器
 */
class CategoryController extends \Our\Controller_AbstractAdmin
{
    /**
     * 分类列表
     */
    public function indexAction()
    {
        $page = $this->getRequest()->getParam('page', 1);
        $count = $this->getRequest()->getParam('count', 12);

        $where = $this->_getWhere();
        $order = $this->_getOrder(array('category_id desc'));

        $where['parent_category_id'] = 0; // 默认取到父类的就可以了。

        $CategoryModel = \Category\CategoryModel::getInstance();
        list($result, $paginator) = $CategoryModel->categoryList([
            'page' => $page,
            'count' => $count,
            'where' => $where,
            'order' => $order,
            'baseUrl' => $this->_getBaseUrl()
        ]);

        $this->getView()->assign('paginator', $paginator);
        $this->getView()->assign('result', $result);
    }

    /**
     * 分类增加功能
     */
    public function addAction()
    {
        $form = new \Category\CategoryAddFormModel();
        if ($this->_isPost()) {
            $form->setData($this->_getPost());
        }

        if ($this->_isPost() && $form->validate()) {
            try {
                $params = $form->getFieldValue();
                $CategoryModel = \Category\CategoryModel::getInstance();
                $result = $CategoryModel->add($params);
                if ($result){
                    $this->redirect("/admin/category/index/");
                } else {
                    echo "增加分类错误";
                }
                return;
            } catch (\Exception $e) {
                $this->_assignErrorMessage($e->getMessage(), $e->getCode());
            }
        }

        $CategoryModel = \Category\CategoryModel::getInstance();
        list($parentCategoryList,) = $CategoryModel->categoryList([
            'page' => 1,
            'count' => 1000,
            'where' => ['parent_category_id' => 0],
            'order' => ['category_id asc'],
        ]);

        $this->_assign('form', $form);
        $this->_assign('parentCategoryList', $parentCategoryList);
    }

    /**
     * 分类编辑功能
     */
    public function editAction()
    {
        $category = $this->_validateCategory($this->_getQuery("category_id"));
        $form = new \Category\CategoryAddFormModel();
        if ($this->_isGet()) {
            $form->setData($category);
        }
        if ($this->_isPost()) {
            $form->setData($this->_getPost());
        }

        if ($this->_isPost() && $form->validate()) {
            try {
                $params = $form->getFieldValue();
                $params['category_id'] = $category['category_id'];

                $CategoryModel = \Category\CategoryModel::getInstance();
                $result = $CategoryModel->edit($params);
                $this->redirect("/admin/category/index/");
                return;
            } catch (Exception $e) {
                if (!($e->getCode() > 10000)) {
                    throw $e;
                }
                $this->_assignErrorMessage($e->getMessage(), $e->getCode());
            }
        }

        $CategoryModel = \Category\CategoryModel::getInstance();
        list($parentCategoryList,) = $CategoryModel->categoryList([
            'page' => 1,
            'count' => 1000,
            'where' => ['parent_category_id' => 0],
            'order' => ['category_id asc'],
        ]);

        $this->_assign('parentCategoryList', $parentCategoryList);
        $this->_assign('form', $form);
        $this->_display("category/add.phtml");
    }

    /**
     * 校验分类是否存在
     *
     * @param int $id
     * @return array
     */
    protected function _validateCategory($id)
    {
        $CategoryDAOModel = \Category\CategoryDAOModel::getInstance();
        $model = $CategoryDAOModel->find($id);
        if (!$model) {
            exit();
        }
        return $model;
    }

}
